#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "Init.h"
#include "drawsubway.h"
#include"qpushbutton.h"
#include "ZoomIO.h"
#include"Dijkstra.h"
#include"QLineEdit"
#include<QMessageBox>
#include"addmenu.h"
#include<qtextcodec.h>
#include "sqliteoperate.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("青岛地铁换乘系统");
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

    flag = 0;
    file_update_MessageBox(0,label,flag);
    SqliteOperate sqliteOperate;
    sqliteOperate.select_all_data_txt();
    infoinit();

    drawsys(ui->graphicsView);
    // 设置滚轮事件追踪，以便可以检测到滚轮事件
    ui->graphicsView->setMouseTracking(true);
    ui->graphicsView->viewport()->setMouseTracking(true);
    ui->graphicsView->setFocusPolicy(Qt::StrongFocus);

    // 设置滚轮事件过滤器
    ui->graphicsView->viewport()->installEventFilter(this);


    QPushButton *zoom_in = new QPushButton(this);
    zoom_in->setIcon(QIcon(":/image/src/zoom_in.svg"));//设置图标
    zoom_in->setToolTip("放大");//设置悬浮文本
    this->setContextMenuPolicy(Qt::NoContextMenu);//禁用上下文菜单
    ui->toolBar->addWidget(zoom_in);//将按钮添加到toolbar
    ui->toolBar->setMovable(false);//设置为不可移动
    connect(zoom_in,&QPushButton::clicked,this,[=]{
        zoomIn(ui->graphicsView);//放大函数
    });

//    ui->toolBar->addSeparator();

    QPushButton *zoom_out = new QPushButton(this);
    zoom_out->setIcon(QIcon(":/image/src/zoom_out.svg"));
    zoom_out->setToolTip("缩小");
    ui->toolBar->addWidget(zoom_out);
    connect(zoom_out,&QPushButton::clicked,this,[=]{
        zoomOut(ui->graphicsView);
    });

    QPushButton *return_subway = new QPushButton(this);
    return_subway->setIcon(QIcon(":/image/src/subway.svg"));
    return_subway->setToolTip("返回地铁线路图 ");
    ui->toolBar->addWidget(return_subway);
    connect(return_subway,&QPushButton::clicked,this,[=]{
        drawsys(ui->graphicsView);
    });
    initcombox(ui->comboBox,ui->comboBox_2);//将内容写入到combobox
    QWidget *stretch = new QWidget();
    stretch->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    ui->toolBar->addWidget(stretch);

    label = new QLabel(" 当前数据版本:"+search_verision()+"  ", this);
    ui->toolBar->addWidget(label);
    QPushButton *update_bt = new QPushButton(this);

    update_bt->setText("检查更新");
    update_bt->setToolTip("检查更新");
    ui->toolBar->addWidget(update_bt);
    connect(update_bt,&QPushButton::clicked,this,[=]{
        file_update_MessageBox(1,label,flag);
    });

    connect(ui->pushButton,&QPushButton::clicked,this,[=]{
        ChooseShortorLessPath(ui->graphicsView,ui->comboBox->lineEdit()->text(),ui->comboBox_2->lineEdit()->text(),
                              ui->radioButton,ui->radioButton_2,ui->textBrowser);

    });
    connect(ui->action_QT,&QAction::triggered,this,[=]{//QAction发射的信号
        QMessageBox::aboutQt(this,tr("关于Qt"));
    });
    connect(ui->action,&QAction::triggered,this,[=]{
        QMessageBox box;
        box.setWindowTitle(tr("关于autor"));
        box.setIcon(QMessageBox::Information);
        box.setText(tr(
            "学校    : 鲁东大学  \n"
            "组别    : 陈硕组  \n"
            "指导老师 : 丁元 \n"
            "成员    : 陈硕、马广武、孟祥远、赵广旭、周子琪 \n"
            ));
        box.addButton(tr("确定"),QMessageBox::AcceptRole);//定义按钮
        if(box.exec() == QMessageBox::Accepted) box.close();
    });

    connect(ui->action_2,&QAction::triggered,this,[=]{
        addMenu * menu = new addMenu();
        menu->setWindowTitle("添加");
        menu->show();
    });
    flag = 1;
  //  ddebug();
}
MainWindow::~MainWindow()
{
    delete ui;
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == ui->graphicsView->viewport() && event->type() == QEvent::Wheel) {
        QWheelEvent *wheelEvent = static_cast<QWheelEvent*>(event);
        if (ctrlPressed) {
            wheelEvent->ignore();
            return true;
        }
    }

    return QMainWindow::eventFilter(obj, event);
}


void MainWindow::wheelEvent(QWheelEvent *event)
{
    QGraphicsView *graphicsView = ui->graphicsView;

    if (event->modifiers() == Qt::ControlModifier) {
        if (event->angleDelta().y() < 0) {
            zoomOut(ui->graphicsView);
            event->accept();
            return;
        } else if (event->angleDelta().y() > 0) {
            zoomIn(ui->graphicsView);
            event->accept();
            return;
        }
    }

    QMainWindow::wheelEvent(event);
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Control) {
        ctrlPressed = true;
        ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
        ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    }

    QMainWindow::keyPressEvent(event);
}

void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Control) {
        ctrlPressed = false;
        QGraphicsView *graphicsView = ui->graphicsView;
        ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
        ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    }

    QMainWindow::keyReleaseEvent(event);

}

